<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Chebychev design of an FIR filter given a desired H(w)</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/filter_design/html/fir_chebychev_design.html">
<link rel="stylesheet" href="../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Chebychev design of an FIR filter given a desired H(w)</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% "Filter design" lecture notes (EE364) by S. Boyd</span>
<span class="comment">% (figures are generated)</span>
<span class="comment">%</span>
<span class="comment">% Designs an FIR filter given a desired frequency response H_des(w).</span>
<span class="comment">% The design is judged by the maximum absolute error (Chebychev norm).</span>
<span class="comment">% This is a convex problem (after sampling it can be formulated as an SOCP).</span>
<span class="comment">%</span>
<span class="comment">%   minimize   max |H(w) - H_des(w)|     for w in [0,pi]</span>
<span class="comment">%</span>
<span class="comment">% where H is the frequency response function and variable is h</span>
<span class="comment">% (the filter impulse response).</span>
<span class="comment">%</span>
<span class="comment">% Written for CVX by Almir Mutapcic 02/02/06</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% problem specs</span>
<span class="comment">%********************************************************************</span>
<span class="comment">% number of FIR coefficients (including the zeroth one)</span>
n = 20;

<span class="comment">% rule-of-thumb frequency discretization (Cheney's Approx. Theory book)</span>
m = 15*n;
w = linspace(0,pi,m)'; <span class="comment">% omega</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% construct the desired filter</span>
<span class="comment">%********************************************************************</span>
<span class="comment">% fractional delay</span>
D = 8.25;            <span class="comment">% delay value</span>
Hdes = exp(-j*D*w);  <span class="comment">% desired frequency response</span>

<span class="comment">% Gaussian filter with linear phase (uncomment lines below for this design)</span>
<span class="comment">% var = 0.05;</span>
<span class="comment">% Hdes = 1/(sqrt(2*pi*var))*exp(-(w-pi/2).^2/(2*var));</span>
<span class="comment">% Hdes = Hdes.*exp(-j*n/2*w);</span>

<span class="comment">%*********************************************************************</span>
<span class="comment">% solve the minimax (Chebychev) design problem</span>
<span class="comment">%*********************************************************************</span>
<span class="comment">% A is the matrix used to compute the frequency response</span>
<span class="comment">% A(w,:) = [1 exp(-j*w) exp(-j*2*w) ... exp(-j*n*w)]</span>
A = exp( -j*kron(w,[0:n-1]) );

<span class="comment">% optimal Chebyshev filter formulation</span>
cvx_begin
  variable <span class="string">h(n,1)</span>
  minimize( max( abs( A*h - Hdes ) ) )
cvx_end

<span class="comment">% check if problem was successfully solved</span>
disp([<span class="string">'Problem is '</span> cvx_status])
<span class="keyword">if</span> ~strfind(cvx_status,<span class="string">'Solved'</span>)
  h = [];
<span class="keyword">end</span>

<span class="comment">%*********************************************************************</span>
<span class="comment">% plotting routines</span>
<span class="comment">%*********************************************************************</span>
<span class="comment">% plot the FIR impulse reponse</span>
figure(1)
stem([0:n-1],h)
xlabel(<span class="string">'n'</span>)
ylabel(<span class="string">'h(n)'</span>)

<span class="comment">% plot the frequency response</span>
H = [exp(-j*kron(w,[0:n-1]))]*h;
figure(2)
<span class="comment">% magnitude</span>
subplot(2,1,1);
plot(w,20*log10(abs(H)),w,20*log10(abs(Hdes)),<span class="string">'--'</span>)
xlabel(<span class="string">'w'</span>)
ylabel(<span class="string">'mag H in dB'</span>)
axis([0 pi -30 10])
legend(<span class="string">'optimized'</span>,<span class="string">'desired'</span>,<span class="string">'Location'</span>,<span class="string">'SouthEast'</span>)
<span class="comment">% phase</span>
subplot(2,1,2)
plot(w,angle(H))
axis([0,pi,-pi,pi])
xlabel(<span class="string">'w'</span>), ylabel(<span class="string">'phase H(w)'</span>)
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling Mosek 9.1.9: 1199 variables, 321 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (990) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (1059) of matrix 'A'.
MOSEK warning 710: #6 (nearly) zero elements are specified in sparse col '' (1197) of matrix 'A'.
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 321             
  Cones                  : 300             
  Scalar variables       : 1199            
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 321             
  Cones                  : 300             
  Scalar variables       : 1199            
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 21
Optimizer  - Cones                  : 300
Optimizer  - Scalar variables       : 899               conic                  : 899             
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 231               after factor           : 231             
Factor     - dense dim.             : 0                 flops                  : 3.82e+05        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   3.0e+02  1.0e+00  1.0e+00  0.00e+00   0.000000000e+00   0.000000000e+00   1.0e+00  0.01  
1   3.7e+01  1.2e-01  3.0e-02  -7.74e-02  -9.404267175e-01  -1.027139074e+00  1.2e-01  0.01  
2   1.4e+01  4.7e-02  9.1e-03  2.82e+00   -6.722914608e-01  -6.681299006e-01  4.7e-02  0.01  
3   2.0e+00  6.8e-03  4.8e-04  1.05e+00   -7.106031190e-01  -7.103719672e-01  6.8e-03  0.02  
4   4.2e-02  1.4e-04  1.4e-06  1.02e+00   -7.071883645e-01  -7.071789603e-01  1.4e-04  0.02  
5   8.1e-05  2.7e-07  1.2e-10  1.00e+00   -7.071069665e-01  -7.071069484e-01  2.7e-07  0.02  
6   1.4e-07  4.7e-10  9.1e-15  1.00e+00   -7.071067815e-01  -7.071067815e-01  4.7e-10  0.02  
7   2.4e-10  1.2e-10  4.0e-17  1.00e+00   -7.071067812e-01  -7.071067812e-01  1.0e-12  0.02  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -7.0710678116e-01   nrm: 1e+00    Viol.  con: 2e-10    var: 0e+00    cones: 0e+00  
  Dual.    obj: -7.0710678119e-01   nrm: 9e-01    Viol.  con: 0e+00    var: 7e-13    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 7         time: 0.02    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.707107
 
Problem is Solved
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="fir_chebychev_design__01.png" alt=""> <img src="fir_chebychev_design__02.png" alt=""> 
</div>
</div>
</body>
</html>